<?php
include_once('common/header.php');
$strRefer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
if($AppUI->id <= 0 || $strRefer == '') {
	$arrJson 	= array('error'=>1,'msg'=>$arrError[3]);
	echo $objRequest->getParam('callback') . '(' . Zend_Json::encode($arrJson) . ')';
	exit();
} 
$intAction 		= $objRequest->getParam('act',0);
$strContent 	= $objRequest->getParam('content','');
$intPlaceId 	= $objRequest->getParam('placeid',0);
$intRating		= $objRequest->getParam('rate', 0);
$intPcommentId	= $objRequest->getParam('pcommentid',0);
$intError		= 0;
$strFullname	= '';
if($intAction == 0 || !isset($intAction)) {
	$strContent = Vfriend_Function::stripTags($strContent,0);
	if(trim($strContent) == '' || !isset($strContent)) $intError = 1;
	elseif(strlen($strContent) < 2) $intError = 2;
}
if($intError == 0) {
	$objPlace 	= new Place();
	$strMessage	= '';
	$intLike	= 0;
	if($intAction == 0 || !isset($intAction)) {	
		$intHasComment 	= 0;
		$intError		= $objPlace->addComment($AppUI->id, $intPlaceId, $strContent, $intRating);
		if($intError == 0) {
			Vfriend_Function::deleteCookie('plcmt' . $intPlaceId);
			$strMessage	= $arrMessage[1];
		} else if($intError == 1){
			$objVfriendUser = new Vfriend_User();
			$objUser		= $objVfriendUser->getFull();
			$strFullname	= $objUser->fullnamevn;
			$intError 		= 14;
		}else {
			$intError 		= 22;
		}	
	}
	elseif(isset($intAction) && $intAction == 1) {
		$intError = $objPlace->deleteComment($AppUI->id, $intPlaceId, $intPcommentId);
		if($intError == 0) {
			$strMessage = $arrMessage[2];
			Vfriend_Function::deleteCookie('plcmt' . $intPlaceId);
		} else {
			$intError = 4;
		}
	}
	/*elseif(isset($intAction) && $intAction == 2) {
		$intError 	= $objPlace->addLikeCmt($AppUI->id, $intPlaceId, $intPcommentId, $intLike);		
		if($intError == 0) {
			$strMessage = $arrMessage[6];
		} else {
			$intError	= $intError+17;
		}
	}*/
	if($intError == 0) {		
		include_once('place/comment.php');
		if($intPlaceId > 0) {
			$this->view->row	= $objPlace->get($intPlaceId);
		}
		$objView	= Zend_Registry::get('objView');
		$strData	= $objView->fetch('place/box-comment.tpl');
		$strRating	= $objView->fetch('place/box-rating.tpl');
		$arrJson	= array('error'	=> 0, 
							'data'	=> $strData,
							'rating'=> $strRating,
							'msg'	=> $strMessage, 
							'total'	=> Vfriend_Function::numberFormat($intTotal)
						);		
		echo $objRequest->getParam('callback') . '(' . Zend_Json::encode($arrJson) . ')';
		exit();
	}
}
if($intError > 0) {
	$arrJson 	= array('error'=>$intError,'msg'=>$arrError[$intError]);
	echo $objRequest->getParam('callback') . '(' . Zend_Json::encode($arrJson) . ')';
	exit();
}
?>
